using UnityEngine;

public class PlayerAfterImageSprite : MonoBehaviour
{
    [SerializeField] private float _activeTime = 0.3f;

    [SerializeField] private float _alphaSet = 0.8f;
    private readonly float _alphaMultiplier = 0.98f;

    private float _alpha;

    private Color _color;
    private SpriteRenderer _playerSR;

    private Transform _playerTransform;

    private SpriteRenderer _SR;
    private float _timeActivated;

    private void Update()
    {
        _alpha *= _alphaMultiplier;
        _color = new Color(1, 1, 1, _alpha);
        _SR.color = _color;

        if (Time.time > _timeActivated + _activeTime) PlayerAfterImagePool.Instance.AddToPool(gameObject);
    }

    public void OnEnable()
    {
        _SR = GetComponent<SpriteRenderer>();
        _playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
        _playerSR = _playerTransform.GetComponent<SpriteRenderer>();

        _alpha = _alphaSet;
        _SR.sprite = _playerSR.sprite;
        transform.position = _playerTransform.position;
        transform.rotation = _playerTransform.rotation;
        _timeActivated = Time.time;
    }
}